//
//  XZMainController.m
//  生活小助手
//
//  Created by lixiangzhou on 14-8-7.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "XZMainController.h"
#import "XZFunctionView.h"

#import "XZWeatherController.h"
#import "ZYIdentityController.h"
#import "XZAppleController.h"
#import "XZCurrencyController.h"
#import "XZFoodController.h"
#import "XZTrainController.h"
#import "XZIPAddressController.h"
#import "XZLotteryController.h"


//#define kFunctionCount 9
#define kFunctionRowCount 3

@interface XZMainController () <XZFunctionViewDelegate>

@end

@implementation XZMainController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"生活小助手";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addFunctionViews];

}

- (void)addFunctionViews
{
    XZFunctionView *weatherReport = [XZFunctionView functionViewWithIcon:@"" title:@"天气预报" type:XZFunctionViewTypeWeather];
    weatherReport.delegate = self;
    [self.view addSubview:weatherReport];
    
    XZFunctionView *lottery = [XZFunctionView functionViewWithIcon:@"" title:@"彩票" type:XZFunctionViewTypeLottery];
    lottery.delegate = self;
    [self.view addSubview:lottery];
    
    XZFunctionView *ipAddress = [XZFunctionView functionViewWithIcon:@"" title:@"ip地址" type:XZFunctionViewTypeIPAddress];
    ipAddress.delegate = self;
    [self.view addSubview:ipAddress];
    
    XZFunctionView *train = [XZFunctionView functionViewWithIcon:@"" title:@"12306" type:XZFunctionViewTypeTrain];
    train.delegate = self;
    [self.view addSubview:train];
    
    XZFunctionView *food = [XZFunctionView functionViewWithIcon:@"" title:@"菜谱" type:XZFunctionViewTypeFood];
    food.delegate = self;
    [self.view addSubview:food];
    
    XZFunctionView *currency = [XZFunctionView functionViewWithIcon:@"" title:@"货币汇率" type:XZFunctionViewTypeCurrency];
    currency.delegate = self;
    [self.view addSubview:currency];
    
    XZFunctionView *apple = [XZFunctionView functionViewWithIcon:@"" title:@"苹果序列号" type:XZFunctionViewTypeApple];
    apple.delegate = self;
    [self.view addSubview:apple];
    
    XZFunctionView *identity = [XZFunctionView functionViewWithIcon:@"" title:@"身份证查询" type:XZFunctionViewTypeIdentity];
    identity.delegate = self;
    [self.view addSubview:identity];

    CGFloat marginX = (self.view.width - kFunctionRowCount * kFunctionWidth) / (kFunctionRowCount + 1);
    CGFloat marginY = 10;
    CGFloat startY = 100;
    for (int i = 0; i < self.view.subviews.count; i++) {
        int row = i / kFunctionRowCount;
        int col = i % kFunctionRowCount;
        XZFunctionView *funcView = self.view.subviews[i];
        funcView.x = marginX + (marginX + funcView.width) * col;
        funcView.y = startY + (marginY + funcView.height) * row;
    }
}

- (void)functionView:(XZFunctionView *)functionView didClickType:(XZFunctionViewType)type
{
    switch (type) {
        case XZFunctionViewTypeWeather: // 天气预报
        {  XZWeatherController *weatherVC = [[XZWeatherController alloc]init];
            [self.navigationController presentViewController:weatherVC animated:YES completion:nil];
//            [self.navigationController pushViewController:[[XZWeatherController alloc] init] animated:YES];
        }
            break;
        case XZFunctionViewTypeLottery:// 彩票
            [self.navigationController pushViewController:[[XZLotteryController alloc] init] animated:YES];
            break;
        case XZFunctionViewTypeIPAddress:// ip地址
            [self.navigationController pushViewController:[[XZIPAddressController alloc] init] animated:YES];
            break;
        case XZFunctionViewTypeTrain:// 12306
            [self.navigationController pushViewController:[[XZTrainController alloc] init] animated:YES];
            break;
        case XZFunctionViewTypeFood:// 菜谱
            [self.navigationController pushViewController:[[XZFoodController alloc] init] animated:YES];
            break;
        case XZFunctionViewTypeCurrency:// 货币汇率
            [self.navigationController pushViewController:[[XZCurrencyController alloc] init] animated:YES];
            break;
        case XZFunctionViewTypeApple:// 苹果序列号
            [self.navigationController pushViewController:[[XZAppleController alloc] init] animated:YES];
            break;
        case XZFunctionViewTypeIdentity:// 身份证查询
            [self.navigationController pushViewController:[[ZYIdentityController alloc] init] animated:YES];
            break;
            
    }
}

@end
